package com.app.ui.view.down;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.app.utiles.other.DLog;

//倒计时的view
public class TimeDownView extends TextView {
    private CountDown countDown;
    private boolean isRun;

    public TimeDownView(Context context) {
        super(context);
        init();
    }

    public TimeDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }


    private void init() {
        countDown = new CountDown(60 * 1000, 1000);
        setGravity(Gravity.CENTER);
        setOnClickListener(new OnClick());
        setTextSize(14);
    }

    private boolean isBgIcon;
    private int[] textColors = new int[]{0xff939699, 0xff939699};
    private int[] bgColors = new int[]{0xffffffff, 0xffffffff};
    private int[] bgIcon = new int[]{0, 0};

    public void setTextColors(int[] textColors) {
        this.textColors = textColors;
    }

    public void setBgColors(int[] bgColors) {
        isBgIcon = false;
        this.bgColors = bgColors;
    }

    public void setBgIcons(int[] bgIcon) {
        isBgIcon = true;
        this.bgIcon = bgIcon;
    }

    //倒计时重置
    public void timeRest() {
        setText("获取验证码");
        setTextColor(textColors[0]);
        if (isBgIcon) {
            setBackgroundResource(bgIcon[0]);
        } else {
            setBackgroundColor(bgColors[0]);
        }
        isRun = false;
    }

    //到计时中
    private void timeData(String data) {
        setText(data);
        setTextColor(textColors[1]);
        if (isBgIcon) {
            setBackgroundResource(bgIcon[1]);
        } else {
            setBackgroundColor(bgColors[1]);
        }
    }

    //开始计时
    public void timeStart() {
        if (isRun) {
            return;
        }
        isRun = true;
        countDown.start();
        DLog.e("---------------","计时开始");

    }

    //停止倒计时
    public void timeStop() {
        if (!isRun) {
            return;
        }
        countDown.cancel();
        isRun = false;
        DLog.e("---------------","计时停止");
    }

    //倒计时
    class CountDown extends CountDownTimer {
        public CountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            isRun = false;
            timeRest();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            String time = "";
            long millis = millisUntilFinished / 1000;
            if (millis < 10) {
                time = "0";
            }
            time += millis;
            timeData(time);
        }
    }

    class OnClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (isRun) {
                return;
            }
            if (onRequestCode == null) {
                return;
            }
            onRequestCode.doRequestCode(TimeDownView.this);
        }
    }

    protected OnRequestCode onRequestCode;

    public void setOnRequestCode(OnRequestCode onRequestCode) {
        this.onRequestCode = onRequestCode;
    }

    public interface OnRequestCode {
        //  发送请求，获取验证码
        void doRequestCode(TimeDownView view);

        //  验证码,验证完成
        void onComplete(String... other);
    }

}